package com.whalehearing.tingyou.api.controller.hearing;

import com.whalehearing.tingyou.common.annotation.DataSource;
import com.whalehearing.tingyou.common.annotation.Log;
import com.whalehearing.tingyou.common.core.controller.BaseController;
import com.whalehearing.tingyou.common.core.domain.AjaxResult;
import com.whalehearing.tingyou.common.enums.BusinessType;
import com.whalehearing.tingyou.common.enums.DataSourceType;
import com.whalehearing.tingyou.domain.HearingInitialValuation;
import com.whalehearing.tingyou.service.IHearingInitialValuationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 测前初评Controller
 *
 * @author zengjun-qxm
 * @date 2024-09-14
 */
@Api(tags = "api测前初评管理")
@RestController
@RequestMapping("/api/hearing/valuation")
public class ApiHearingInitialValuationController extends BaseController {
    @Autowired
    private IHearingInitialValuationService hearingInitialValuationService;

    /**
     * 根据-userId-查询用户测前初评列表
     */
    @ApiOperation("根据-userId-查询用户测前初评列表")
    @GetMapping(value = "/getInitialValuationListByUserId/{userId}")
    public AjaxResult getInitialValuationListByUserId(@PathVariable Long userId) {
        List<HearingInitialValuation> initialValuationList = hearingInitialValuationService.getInitialValuationListByUserId(userId);
        return success(initialValuationList);
    }

    /**
     * 根据-测前初评id-获取测前初评详细信息
     */
    @ApiOperation("根据-测前初评id-获取测前初评详细信息")
    @GetMapping(value = "/getInitialValuationInfo/{initialValuationId}")
    public AjaxResult getInfo(@PathVariable("initialValuationId") Long initialValuationId)
    {
        return success(hearingInitialValuationService.getById(initialValuationId));
    }

    /**
     * 新增测前初评
     */
    @ApiOperation("新增测前初评")
    @Log(title = "测前初评", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody HearingInitialValuation hearingInitialValuation) {
        boolean isSaved = hearingInitialValuationService.save(hearingInitialValuation);
        //返回主键听力档案id
        if (isSaved && hearingInitialValuation.getInitialValuationId() != null) {
            return AjaxResult.success(hearingInitialValuation.getInitialValuationId());
        } else {
            return AjaxResult.error("插入失败");
        }
    }

    /**
     * 删除测前初评
     */
    @ApiOperation("根据-测前初评id-删除测前初评")
    @Log(title = "测前初评", businessType = BusinessType.DELETE)
    @DeleteMapping("/{initialValuationIds}")
    public AjaxResult remove(@PathVariable Long[] initialValuationIds) {
        return toAjax(hearingInitialValuationService.removeByIds(Arrays.asList(initialValuationIds)));
    }
}
